package com.jsb.laundry.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsb.laundry.common.exception.*;
import com.jsb.laundry.model.dto.posts.PostUserFansQueryRequest;
import com.jsb.laundry.model.dto.posts.PostUserFollowQueryRequest;
import com.jsb.laundry.model.dto.posts.PostUserFollowerAddRequest;
import com.jsb.laundry.model.dto.posts.PostUserFollowersQueryRequest;
import com.jsb.laundry.model.entity.JsbPosts;
import com.jsb.laundry.model.entity.UserDetail;
import com.jsb.laundry.model.vo.PostsUserFollowersVO;
import com.jsb.laundry.model.vo.PostsVO;
import com.jsb.laundry.service.JsbPostUserFollowersService;
import com.jsb.laundry.service.JsbPostsService;
import com.jsb.laundry.service.UserDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 帖子用户关注关系接口
 */
@RestController
@RequestMapping("/posts/user_followers")
@Api(value = "帖子相关接口",tags = "帖子")
@Slf4j
public class PostUserFollowersController {

    @Resource
    private JsbPostUserFollowersService postUserFollowersService;

    @Resource
    private UserDetailService userDetailService;

    @Resource
    private JsbPostsService postService;

    /**
     *  关注/取消关注
     *
     * @param postUserFollowerAddRequest
     * @param request
     * @return resultNum 本次关注变化数
     */
    @PostMapping("/follow")
    @ApiOperation("关注/取消关注")
    public BaseResponse<Integer> doFollow(@RequestBody PostUserFollowerAddRequest postUserFollowerAddRequest, HttpServletRequest request) {
        if (postUserFollowerAddRequest == null || StringUtils.isAnyBlank(postUserFollowerAddRequest.getUserId(), postUserFollowerAddRequest.getFollowerId())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userId = postUserFollowerAddRequest.getUserId();
        String followerId = postUserFollowerAddRequest.getFollowerId();
        int result = postUserFollowersService.doPostUserFollow(userId, followerId);
        return ResultUtils.success(result);
    }

    /**
     * 获取我关注的用户列表
     *
     * @param postUserFollowQueryRequest
     * @param request
     */
    @PostMapping("/list")
    @ApiOperation("获取指定用户关注的用户列表")
    public BaseResponse<Page<PostsUserFollowersVO>> listFollowByPage(@RequestBody PostUserFollowQueryRequest postUserFollowQueryRequest,
                                                                         HttpServletRequest request) {
        if (postUserFollowQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = postUserFollowQueryRequest.getPageNum();
        long size = postUserFollowQueryRequest.getPageSize();
        String userId = postUserFollowQueryRequest.getUserId();
        // 限制爬虫
        Page<UserDetail> userFollowPage = userDetailService.listUserFollowByPage(new Page<>(current, size), userDetailService.getFollowQueryWrapper(postUserFollowQueryRequest), userId);
        return ResultUtils.success(postUserFollowersService.listUserFollowVOPage(userId, userFollowPage, request));
    }

    /**
     * 获取我的粉丝用户列表
     *
     * @param postUserFansQueryRequest
     * @param request
     */
    @PostMapping("/fans_list")
    @ApiOperation("获取我的粉丝用户列表")
    public BaseResponse<Page<PostsUserFollowersVO>> listFansByPage(@RequestBody PostUserFansQueryRequest postUserFansQueryRequest,
                                                                     HttpServletRequest request) {
        if (postUserFansQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = postUserFansQueryRequest.getPageNum();
        long size = postUserFansQueryRequest.getPageSize();
        String userId = postUserFansQueryRequest.getUserId();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20 || userId == null, ErrorCode.PARAMS_ERROR);
        Page<UserDetail> userFansPage = userDetailService.listUserFansByPage(new Page<>(current, size), userDetailService.getFansQueryWrapper(postUserFansQueryRequest), userId);
        return ResultUtils.success(postUserFollowersService.listUserFansVOPage(userId, userFansPage, request));
    }

    /**
     * 获取用户关注的人的帖子列表
     *
     * @param postUserFollowersQueryRequest
     * @param request
     */
    @PostMapping("/posts_list")
    @ApiOperation("获取用户关注的人的帖子列表")
    public BaseResponse<Page<PostsVO>> listFollowPostByPage(@RequestBody PostUserFollowersQueryRequest postUserFollowersQueryRequest,
                                                            HttpServletRequest request) {
        if (postUserFollowersQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = postUserFollowersQueryRequest.getPostQueryRequest().getPageNum();
        long size = postUserFollowersQueryRequest.getPostQueryRequest().getPageSize();
        String userId = postUserFollowersQueryRequest.getUserId();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20 || userId == null, ErrorCode.PARAMS_ERROR);
        Page<JsbPosts> postPage = postUserFollowersService.listFollowPostByPage(new Page<>(current, size),
                postService.getQueryWrapper(postUserFollowersQueryRequest.getPostQueryRequest()), userId);
        return ResultUtils.success(postService.getPostVOPage(userId, postPage, request));
    }
}
